<?php
/**
 * File containing the RandomEMailProcessor
 *
 * PHP Version 5
 *
 * @category Processors
 * @package  Processors
 * @author   Martin Schlecht <herrlollinger@gmail.com>
 * @license  MIT License
 * @link     http://code.google.com/p/universal-test-data
 */

/**
 * Processor class "RandomEMailProcessor"
 *
 * Processor which is used to process tags which implements random e-mail addresses within the text.
 *
 * @category   Processors
 * @package    Processors
 * @subpackage EMailProcessors
 * @author     Martin Schlecht <herrlollinger@gmail.com>
 * @copyright  see @author
 * @license    MIT License
 * @link       http://code.google.com/p/universal-test-data
 *
 * The followings are the available attributes of the processor class
 * @property string $text, the text which should be processed
 */
class RandomEMailProcessor
{
	private $text;
	
	/**
	 * The processor adds random generated e-mail addresses and repeats the numbers n times
	 *
	 * @param String $text, the text to process
	 *
	 * @return String
	 */
	public function process($text) {
		$this->text = $text;
		
		preg_match_all("#\s*{(.*?)}#si", $this->text, $matches);
		
		foreach ($matches[1] AS $m) {
			$string = "{" . $m . "}";
			$m = explode(":", $m);
			
			/**
			 * {randomEMail:domain}
			 * Random e-mails from the domain example.com
			 */
			if ($m[0] == "randomEMail") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || $parameter[0] == "") {
					$parameter[0] = "example.com";
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 1;
				}
				
				$this->text = str_replace($string, $this->manageRepeation($this->getRandomEMail() . "@" . $parameter[0], $string, $parameter[1]), $this->text);
			}
			
			/**
			 * {randomEMailName:example.com}
			 * Random e-mails from the domain example.com with name layout
			 */
			if ($m[0] == "randomEMailName") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || $parameter[0] == "") {
					$parameter[0] = "domain";
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 1;
				}
			
				$this->text = str_replace($string, $this->manageRepeation($this->getRandomEMailName() . "@" . $parameter[0], $string, $parameter[1]), $this->text);				
			}
			
		/**
			 * {randomEMailLastname:domain}
			 * Random e-mails from the domain example.com with lastname layout
			 */
			if ($m[0] == "randomEMailLastname") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || $parameter[0] == "") {
					$parameter[0] = "example.com";
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 1;
				}
			
				$this->text = str_replace($string, $this->manageRepeation($this->getRandomEMailLastname() . "@" . $parameter[0], $string, $parameter[1]), $this->text);
			}
		}
		
		return $this->text;
	}
	
	/**
	 * Returns a random email address with default lenght 8
	 * 
	 * @return int
	 */
	public function getRandomEMail($length = 8) {
		$randomStringProcessor = new RandomStringProcessor();
		return strtolower($randomStringProcessor->randomString($length));
	}
	
	/**
	 * Returns a random email address with default lenght 8
	 * 
	 * @return int
	 */
	public function getRandomEMailName() {
		$randomNameProcessor = new RandomNameProcessor();
		return strtolower($randomNameProcessor->getFirstname() . "." . $randomNameProcessor->getLastname());
	}
	
	/**
	 * Returns a random email address with default lenght 8
	 * 
	 * @return int
	 */
	public function getRandomEMailLastname() {
		$randomNameProcessor = new RandomNameProcessor();
		return strtolower($randomNameProcessor->getLastname());
	}
	
	/**
	 * Manages the repeation of values
	 * 
	 * @param String $value,  the value which should be replaced
	 * @param String $string, the string for the cockie
	 * @param String $times,  the number of times the value should be repeated
	 * 
	 * @return String
	 */
	public function manageRepeation($value, $string, $times) {
		$replacement = "";
		
		if (Yii::app()->session[$string . "_times"] >= $times || !isset(Yii::app()->session[$string . "_times"])) {
			Yii::app()->session[$string . "_replacement"] = $value;
			Yii::app()->session[$string . "_times"] = 1;
							
			$replacement = $value;
		} else {
			$number = Yii::app()->session[$string . "_replacement"];
			$times = Yii::app()->session[$string . "_times"] + 1;
			
			Yii::app()->session[$string . "_times"] = $times;
			
			$replacement = $number;
		}
		
		Yii::app()->session['resetManager']->add($string . "_replacement");
		Yii::app()->session['resetManager']->add($string . "_times");
		
		return $replacement;
	}
	
	/**
	 * Returns the documentation
	 *
	 * @return String
	 */
	 public function documentation() {
		return array(
			array('{randomEMail:domain,times}', 'Random e-mails from the domain example.com, n times repeated'),
			array('{randomEMailName:domain,times}', 'Random e-mails from the domain example.com with name layout, n times repeated'),
			array('{randomEMailLastname:domain,times}', 'Random e-mails from the domain example.com with lastname layout, n times repeated'),  
		);
	 }
	 
	/**
	 * Prints the documentation
	 *
	 * @return string
	 */
	public function print_documentation() {
		foreach ($this->documentation() AS $docu) {
			echo '<span style="font-family:monospace;font-size:11px">' . $docu[0] . '</span><br/>' . $docu[1] . '<br/>';
		}
	}
}
?>